﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Wicresoft.SMS.Data;
using Wicresoft.SMS.Common;
using System.Collections;

namespace Wicresoft.SMS.Gateway.Host
{
    public class AppConfigProvider : IConnectStringProvider, IServiceConfigProvider, IWESMSParams, IExchangeServerInfo
    {
        private int _SendMsgTaskEnable = -1;
        private int _SendMsgTaskSpan = -1;
        private int _QueryMsgTaskEnable = -1;
        private int _QueryMsgTaskSpan = -1;
        private int _SendEmailTaskEnable = -1;
        private int _SendEmailTaskSpan = -1;
        private int _QueryEmailTaskEnable = -1;
        private int _QueryEmailTaskSpan = -1;
        private string _connectionString;
        private string _sTokenString;
        private int _formatID = -1;
        private string _webUrl;

        string _ESUsername;
        string _ESPassword;
       // string _ESDomain;
        string _ESEmail;
       // string _ESServerURL;
       // string _ESServerVision;
        string _ESHost;
        string _ESPort;



        public int SendMsgTaskEnable
        {
            get
            {
                if (_SendMsgTaskEnable == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/sendmsgtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _SendMsgTaskEnable = Convert.ToInt32(keys[0]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _SendMsgTaskEnable;
            }
        }

        public int SendMsgTaskSpan
        {
            get
            {
                if (_SendMsgTaskSpan == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/sendmsgtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _SendMsgTaskSpan = Convert.ToInt32(keys[1]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _SendMsgTaskSpan;
            }
        }

        public int QueryMsgTaskEnable
        {
            get
            {
                if (_QueryMsgTaskEnable == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/querymsgtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _QueryMsgTaskEnable = Convert.ToInt32(keys[0]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _QueryMsgTaskEnable;
            }
        }

        public int QueryMsgTaskSpan
        {
            get
            {
                if (_QueryMsgTaskSpan == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/querymsgtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _QueryMsgTaskSpan = Convert.ToInt32(keys[1]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _QueryMsgTaskSpan;
            }
        }

        #region 邮件TASK相关
        public int SendEmailTaskEnable
        {
            get
            {
                if (_SendEmailTaskEnable == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/sendemailtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _SendEmailTaskEnable = Convert.ToInt32(keys[0]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _SendEmailTaskEnable;
            }
        }

        public int SendEmailTaskSpan
        {
            get
            {
                if (_SendEmailTaskSpan == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/sendemailtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _SendEmailTaskSpan = Convert.ToInt32(keys[1]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _SendEmailTaskSpan;
            }
        }

        public int QueryEmailTaskEnable
        {
            get
            {
                if (_QueryEmailTaskEnable == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/queryemailtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _QueryEmailTaskEnable = Convert.ToInt32(keys[0]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _QueryEmailTaskEnable;
            }
        }

        public int QueryEmailTaskSpan
        {
            get
            {
                if (_QueryEmailTaskSpan == -1)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("task/queryemailtask");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _QueryEmailTaskSpan = Convert.ToInt32(keys[1]);
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _QueryEmailTaskSpan;
            }
        }
        #endregion

        public string ConnectionString
        {
            get
            {
                if (_connectionString == null)
                {
                    try
                    {
                        _connectionString = ConfigurationManager.ConnectionStrings["smsConnector"].ConnectionString;
                    }
                    catch (Exception ex)
                    { Logger.Error("connectionStringError:" + ex); }
                }
                return _connectionString;
            }
        }

        public string WebUrl
        {
            get
            {
                if (_webUrl == null)
                {
                    try
                    {
                        _webUrl = ConfigurationManager.AppSettings["WebUrl"];
                    }
                    catch (Exception ex)
                    { Logger.Error("connectionStringError:" + ex); }
                }
                return _webUrl;
            }
        }

        public string StokenString
        {
            get
            {
                if (_sTokenString == null)
                {
                    try
                    {
                        _sTokenString = ConfigurationManager.AppSettings["sToken"];
                    }
                    catch { }
                }
                return _sTokenString;
            }
        }

        public int formatID
        {
            get
            {
                if (_formatID == -1)
                {
                    try
                    {
                        _formatID = Convert.ToInt32(ConfigurationManager.AppSettings["formatID"]);
                    }
                    catch { }
                }
                return _formatID;
            }
        }

        #region 邮件帐号配置相关
        public string ESUsername
        {
            get
            {
                if (_ESUsername == null)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _ESUsername = keys[3];
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _ESUsername;
            }
        }

        public string ESPassword
        {
            get
            {
                if (_ESPassword == null)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _ESPassword = keys[4];
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _ESPassword;
            }
        }

        //public string ESDomain
        //{
        //    get
        //    {
        //        if (_ESDomain == null)
        //        {
        //            try
        //            {
        //                IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
        //                string[] keys = new string[threadconfig.Keys.Count];
        //                threadconfig.Values.CopyTo(keys, 0);
        //                _ESDomain = keys[1];
        //            }
        //            catch (Exception ex)
        //            {
        //                Logger.Error(ex.Message);
        //            }
        //        }
        //        return _ESDomain;
        //    }
        //}

        public string ESEmail
        {
            get
            {
                if (_ESEmail == null)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _ESEmail = keys[2];
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _ESEmail;
            }
        }

        //public string ESServerURL
        //{
        //    get
        //    {
        //        if (_ESServerURL == null)
        //        {
        //            try
        //            {
        //                IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
        //                string[] keys = new string[threadconfig.Keys.Count];
        //                threadconfig.Values.CopyTo(keys, 0);
        //                _ESServerURL = keys[0];
        //            }
        //            catch (Exception ex)
        //            {
        //                Logger.Error(ex.Message);
        //            }
        //        }
        //        return _ESServerURL;
        //    }
        //}

        //public string ESServerVersion
        //{
        //    get
        //    {
        //        if (_ESServerVision == null)
        //        {
        //            try
        //            {
        //                IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
        //                string[] keys = new string[threadconfig.Keys.Count];
        //                threadconfig.Values.CopyTo(keys, 0);
        //                _ESServerVision = keys[2];
        //            }
        //            catch (Exception ex)
        //            {
        //                Logger.Error(ex.Message);
        //            }
        //        }
        //        return _ESServerVision;
        //    }
        //}

        public string ESHost
        {
            get
            {
                if (_ESHost == null)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _ESHost = keys[1];
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _ESHost;
            }
        }

        public string ESPort
        {
            get
            {
                if (_ESPort == null)
                {
                    try
                    {
                        IDictionary threadconfig = (IDictionary)ConfigurationManager.GetSection("exchangeaccount");
                        string[] keys = new string[threadconfig.Keys.Count];
                        threadconfig.Values.CopyTo(keys, 0);
                        _ESPort = keys[0];
                    }
                    catch (Exception ex)
                    {
                        Logger.Error(ex.Message);
                    }
                }
                return _ESPort;
            }
        }
        #endregion








        public string HRConnectionString
        {
            get { throw new NotImplementedException(); }
        }
    }
}
